package com.dx.controller;
import com.dx.api.RemoteQaLogService;
import com.dx.domain.QaOperLog;
import com.dx.entity.dto.InterfaceDto;
import com.dx.entity.dto.InterfaceQueryDto;
import com.dx.result.R;
import com.dx.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.dx.service.InterfaceOpService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/interface")
@RequiredArgsConstructor
@Api(tags = "接口管理")
public class InterfaceOpController {
    private final InterfaceOpService interfaceOpService;
    private final LogService logService;

    /**
     * 查看接口详情
     * @param id
     * @ return InterfaceDto
     */
    @GetMapping
    @ApiOperation("查看接口详情")
    public InterfaceDto getInterface(Long id) {
        QaOperLog qaOperLog=new QaOperLog();
        qaOperLog.setQuestion("接口调用");
        qaOperLog.setAnswer("接口调用成功");

        qaOperLog.setCreatedBy(1L);
        qaOperLog.setCreatedTime(LocalDateTime.now());
        logService.add(qaOperLog);
        InterfaceDto interfaceDto =interfaceOpService.getInterface(id);
        System.out.println("interfaceDto = " + interfaceDto);
        return interfaceDto;
    }
    /**
     * 录入接口信息
     */
    @PostMapping
    @ApiOperation("录入接口信息")
    public R<?> insertInterface(@RequestBody InterfaceDto interfaceDto ){
        interfaceOpService.insertInterface(interfaceDto);
        return R.success("插入成功");
    }
    /**
     * 删除接口信息
     * @param id
     */
    @PostMapping("/delete")
    @ApiOperation("删除接口信息")
    public R<?> deleteInterface(Long id) {
        interfaceOpService.deleteInterface(id);
        return R.success("删除成功");
    }
    /**
     * 测试接口
     */
    @PostMapping("/test")
    @ApiOperation("晓搭接口模拟，用于测试")
    public R<?> testInterface(@RequestBody R<?> requestBody, @RequestHeader String token,@RequestHeader String type,Long id) {
        //模拟接口的成功和失败情况
        //产生一个0-2的随机数
        int randomNumber = (int) (Math.random() * 3);
        System.out.println("randomNumber = " + randomNumber);

        if (randomNumber<2) {
            return R.fail("接口调用失败");
        }
        System.out.println("开始所配置接口的调用");
        System.out.println("请求体为"+requestBody);
        System.out.println("请求头信息如下");
        System.out.println("token为" + token);
        System.out.println("type = " + type);
        System.out.println("请求参数信息如下");
        System.out.println("id = " + id);
        System.out.println("接口调用成功");
        return R.success("测试成功");
    }


    /**
     * 修改接口信息
     * 更新策略采用用户将新的接口信息传入，对于基础接口信息采用update方式
     * 对于接口关联的参数和头采用delete和insert方式
     */
    @PostMapping("/update")
    @ApiOperation("修改接口信息")
    public R<?> updateInterface(@RequestBody InterfaceDto interfaceDto) {
        interfaceOpService.updateInterface(interfaceDto);
        return R.success("更新成功");
    }
    /**
     * 执行接口
     */
    @PostMapping("/execute")
    @ApiOperation("执行接口")

    public R<?> executeInterface(Long id) {
        Object o=interfaceOpService.executeInterface(id);
        return R.success("执行成功",o);
    }
    /**
     * pagehelper分页获取接口列表
     */
    @GetMapping("/list")
    @ApiOperation("pagehelper分页获取接口列表")
    public R<?> list(InterfaceQueryDto interfaceQueryDto) {
        return R.success(interfaceOpService.list(interfaceQueryDto));
    }


}
